﻿using System.Threading;
using System.Threading.Tasks;

namespace ESRI.ArcGIS.Client.Tasks.Async
{
    public static partial class IdentifyTaskExtensions
    {
        #region ExecuteTask

        /// <summary>
        /// Executes an identify against an ArcGIS Server map layer.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="identifyParameters">Specifies the criteria used to identify the features.</param>
        /// <returns></returns>
        public static Task<IdentifyTaskResult> ExecuteTaskAsync(this IdentifyTask task,
            IdentifyParameters identifyParameters)
        {
            return ExecuteTaskAsync(task, identifyParameters, CancellationToken.None);
        }

        /// <summary>
        /// Executes an identify against an ArcGIS Server map layer.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="identifyParameters">Specifies the criteria used to identify the features.</param>
        /// <param name="cancellationToken">Cancellation token for cancelling task.</param>
        /// <returns></returns>
        public static Task<IdentifyTaskResult> ExecuteTaskAsync(this IdentifyTask task,
            IdentifyParameters identifyParameters,
            CancellationToken cancellationToken)
        {
            return task.HandleTask<IdentifyTask, IdentifyEventArgs, IdentifyTaskResult>(
                t => t.ExecuteAsync(identifyParameters, null),
                (t, e) => t.ExecuteCompleted += e,
                (t, e) => t.ExecuteCompleted -= e,
                e => new IdentifyTaskResult(e), 
                cancellationToken);
        }

        #endregion

    }
}
